unit uTweenButtons;

interface
uses
  GlMaterial, GlRenderContextInfo,
  VectorGeometry,
  uBaseButton,
  uTweener;

type
  TGUICustomTweenButton = class(TGUICustomHudButton)
  protected
    fTweener: TTweener;
    fTweenParam: Single;
  public
    property Tweener: TTweener read fTweener write fTweener;
  end;

  TGUIScaleButton = class(TGUICustomTweenButton)
  protected
    fBaseSize: TVector;
    fScale: Single;
  public
    Procedure DoRender(var ARci: TRenderContextInfo; ARenderSelf, ARenderChildren: Boolean); override;
    Procedure Activate; override;
    Procedure DeActivate; override;
    Procedure SetMaterial(fButtonMaterialName: String; aMatLib: TGlMaterialLibrary);
  end;

  TGUISliderButton = class(TGUICustomTweenButton)
  protected
    fSlidePos: Single;
    fButtonMaterial: TGlMaterial;
    fSliderMaterial: TGlMaterial;
  protected
    Procedure PrepareButtonPosition; virtual;
    Procedure PrepareSliderPosition; virtual;
  public
    Procedure Activate; override;
    Procedure DeActivate; override;
    Procedure DoRender(var ARci: TRenderContextInfo; ARenderSelf, ARenderChildren: Boolean); override;
    Procedure SetMaterials(fButtonMaterialName: String; fSliderMaterialName: String; aMatLib: TGlMaterialLibrary);
  end;

  TGUI3dSlideButton = class(TGUISliderButton)
  protected
    Procedure PrepareButtonPosition; override;
    Procedure PrepareSliderPosition; override;
  end;
implementation

uses GLScene;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Procedure TGUIScaleButton.DoRender(var ARci: TRenderContextInfo; ARenderSelf, ARenderChildren: Boolean);
begin
  fHudSprite.Position.x := Position.x;
  fHudSprite.Position.y := Position.y;

  fHudSprite.Width := fBaseSize[0] * fScale;
  fHudSprite.Height := fBaseSize[0] * fScale;

  fHudSprite.DoRender(ARci, true, false);
end;
//------------------------------------------------------------------------------
Procedure TGUIScaleButton.Activate;
begin
  inherited Activate;
  fTweener.DeletePSingle(@fScale);
  fTweener.AddTweenPSingle(@fScale, ts_ElasticEaseIn, fScale, 1.2, 1.5);
end;
//------------------------------------------------------------------------------
Procedure TGUIScaleButton.DeActivate;
begin
  inherited DeActivate;
  fTweener.DeletePSingle(@fScale);
  fTweener.AddTweenPSingle(@fScale, ts_ElasticEaseIn, fScale, 1, 1);
end;
//------------------------------------------------------------------------------
Procedure TGUIScaleButton.SetMaterial(fButtonMaterialName: String; aMatLib: TGlMaterialLibrary);
begin
  fHudSprite.Material.LibMaterialName := fButtonMaterialName;
  fHudSprite.Material.MaterialLibrary := aMatLib;
  SetRectHitAreaFromTexture(fHudSprite.Material.GetActualPrimaryMaterial);
  fBaseSize := VectorMake(fHudSprite.Material.GetActualPrimaryTexture.Image.Width,
                          fHudSprite.Material.GetActualPrimaryTexture.Image.Height, 0);
  fScale := 1;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Procedure TGUISliderButton.PrepareButtonPosition;
begin
  fHudSprite.Position.x := Position.x + fSlidePos*90*(fAnimationTime - cst_animTimeShow) / (cst_animTimeActive - cst_animTimeShow);
end;
//------------------------------------------------------------------------------
Procedure TGUISliderButton.PrepareSliderPosition;
begin
  fHudSprite.Position.x := Position.x;
end;
//------------------------------------------------------------------------------
Procedure TGUISliderButton.Activate;
begin
  inherited Activate;
  fTweener.DeletePSingle(@fSlidePos);
  fTweener.AddTweenPSingle(@fSlidePos, ts_ElasticEaseIn, fSlidePos, 1, 2);
end;
//------------------------------------------------------------------------------
Procedure TGUISliderButton.DeActivate;
begin
  inherited Activate;
  fTweener.DeletePSingle(@fSlidePos);
  fTweener.AddTweenPSingle(@fSlidePos, ts_ExpoEaseIn, fSlidePos, 0, 1.5);
end;
//------------------------------------------------------------------------------
Procedure TGUISliderButton.DoRender(var ARci: TRenderContextInfo; ARenderSelf, ARenderChildren: Boolean);
begin
  fHudSprite.Position.x := Position.x;
  fHudSprite.Position.y := Position.y;

  PrepareButtonPosition;
  fHudSprite.Material := fSliderMaterial;
  fHudSprite.Width := fSliderMaterial.Texture.Image.Width;
  fHudSprite.Height := fSliderMaterial.Texture.Image.Height;
  fHudSprite.DoRender(ARci, true, false);

  PrepareSliderPosition;
  fHudSprite.Material := fButtonMaterial;
  fHudSprite.Width := fButtonMaterial.Texture.Image.Width;
  fHudSprite.Height := fButtonMaterial.Texture.Image.Height;
  fButtonMaterial.FrontProperties.Diffuse.Alpha := 1;
  fHudSprite.DoRender(ARci, true, false);
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Procedure TGUISliderButton.SetMaterials(fButtonMaterialName: String; fSliderMaterialName: String; aMatLib: TGlMaterialLibrary);
begin
  fButtonMaterial := aMatLib.Materials.GetLibMaterialByName(fButtonMaterialName).Material;
  fSliderMaterial := aMatLib.Materials.GetLibMaterialByName(fSliderMaterialName).Material;
  SetRectHitAreaFromTexture(fButtonMaterial);
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Procedure TGUI3dSlideButton.PrepareButtonPosition;
begin
  fHudSprite.Position.x := Position.x;
end;
//------------------------------------------------------------------------------
Procedure TGUI3dSlideButton.PrepareSliderPosition;
begin
  fHudSprite.Position.x := fHudSprite.Position.x + 5 + 3*fSlidePos;
  fHudSprite.Position.y := fHudSprite.Position.y - 5 - 3*fSlidePos;
end;
//------------------------------------------------------------------------------
end.
